<template>
      <div id="loading-mask">
          <div class="loading-wrapper">
            <span class="loading-dot loading-dot-spin">
              <i></i><i></i><i></i><i></i>
            </span>
          </div>
        </div>
</template>


<style>
      #loading-mask {
        position: fixed;
        left: 0;
        top: 0;
        height: 100%;
        width: 100%;
        background: #fff;
        user-select: none;
        z-index: 999;
        overflow: hidden;
      }
      .loading-wrapper {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -100%);
      }
      .loading-dot {
        animation: antRotate 1.2s infinite linear;
        transform: rotate(45deg);
        position: relative;
        display: inline-block;
        font-size: 64px;
        width: 64px;
        height: 64px;
        box-sizing: border-box;
      }
      .loading-dot i {
        width: 22px;
        height: 22px;
        position: absolute;
        display: block;
        background-color: #1890ff;
        border-radius: 100%;
        transform: scale(0.75);
        transform-origin: 50% 50%;
        opacity: 0.3;
        animation: antSpinMove 1s infinite linear alternate;
      }
      .loading-dot i:nth-child(1) {
        top: 0;
        left: 0;
      }
      .loading-dot i:nth-child(2) {
        top: 0;
        right: 0;
        -webkit-animation-delay: 0.4s;
        animation-delay: 0.4s;
      }
      .loading-dot i:nth-child(3) {
        right: 0;
        bottom: 0;
        -webkit-animation-delay: 0.8s;
        animation-delay: 0.8s;
      }
      .loading-dot i:nth-child(4) {
        bottom: 0;
        left: 0;
        -webkit-animation-delay: 1.2s;
        animation-delay: 1.2s;
      }
      @keyframes antRotate {
        to {
          -webkit-transform: rotate(405deg);
          transform: rotate(405deg);
        }
      }
      @-webkit-keyframes antRotate {
        to {
          -webkit-transform: rotate(405deg);
          transform: rotate(405deg);
        }
      }
      @keyframes antSpinMove {
        to {
          opacity: 1;
        }
      }
      @-webkit-keyframes antSpinMove {
        to {
          opacity: 1;
        }
      }
</style>

<script>
function getUrlKey(name){//获取url 参数

  return decodeURIComponent((new RegExp('[?|&]'+name+'='+'([^&;]+?)(&|#|;|$)').exec(location.href)||[,""])[1].replace(/\+/g,'%20'))||null;

}

function getCodeApi(state){//获取code

    let urlNow=encodeURIComponent(window.location.href);

    let scope='snsapi_base'; //snsapi_userinfo //静默授权 用户无感知

    let appid='wx304b179d66268e6b';

    let url=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;

    window.location.replace(url);

}


import { getCodeImg,getOpenId } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import { getFromInfo } from '@/api/index.js'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
   
    
    let code=getUrlKey("code");//获取url参数code
    if(code){ //拿到code， code传递给后台接口换取opend
      let that = this;
      getOpenId(code).then(res=>{
        if(res.code==200)
        {
          let projectId=getUrlKey("projectid");//获取url参数projectid
          //that.$store.dispatch("setProjectId",projectId);
          //that.$store.dispatch("setOpenId",res.msg);
          localStorage.setItem("projectId",projectId);
          localStorage.setItem("openId",res.msg);

          //如果没有传项目id，跳转到项目选择界面
          if(projectId===''||projectId==null)
          {
            that.$router.push({ path: "/selectprj"});
          }
          else
          {
             //获取项目信息和当前用户是否有登记的记录
             let param = {'fromId':projectId,'openId':res.msg};
             getFromInfo(param).then(res=>{
                console.log('formInfo',res)
                if(res.code==0)
                {
                  //that.$store.dispatch("setFormInfo",res.data);

                  localStorage.setItem("formInfo",JSON.stringify(res.data));

                  if(res.data.dataId!=null)
                  {

                    that.$router.push({ path: "/viewfrom?tablefid="+res.data.dataId});
                  }
                  else{
                    if(res.data.isWhite=='是')
                    {
                      that.$router.push({ path: "/checkidn"});
                    }else
                    {
                      that.$router.push({ path: "/viewfrom"}); 
                    }
                  }
                }
                else
                {
                   that.$message.error('系统开小差了，请重试')
                   localStorage.setItem("projectId",'');
                   localStorage.setItem("openId",'');
                   localStorage.setItem("formInfo",'');

                }
             });
          }
          
        }else
        {
           that.$message.error('获取用户编号失败！')
           //that.$store.dispatch("setOpenId","");
           //that.$store.dispatch("setProjectId","");
           localStorage.setItem("projectId",'');
           localStorage.setItem("openId",'');
           localStorage.setItem("formInfo",'');
        }
        

      }).catch(res=>{
        
        that.$message.error('获取用户编号失败！')
        //that.$store.dispatch("setOpenId","");
        //that.$store.dispatch("setProjectId","");
        localStorage.setItem("projectId",'');
        localStorage.setItem("openId",'');
        localStorage.setItem("formInfo",'');
        setTimeout(function(){
           that.$router.push({ path: "/reg"});
            getCodeApi("_wx");
        },200)
      })
      

    }else{

      getCodeApi("_wx");

    }

  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      
      this.$router.push({ path: "/viewfrom"});
    }
  }
};
</script>